/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui.listgrid;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.MouseOutEvent;
import com.smartgwt.client.widgets.events.MouseOutHandler;
import com.smartgwt.client.widgets.events.MouseOverEvent;
import com.smartgwt.client.widgets.events.MouseOverHandler;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.spotimage.catalog.client.event.MoveToSelectionEvent;
import com.spotimage.catalog.client.event.EventBus;
import com.spotimage.catalog.client.event.RemoveFromSelectionEvent;
import com.spotimage.catalog.client.ui.SceneDetailsWindow;
import com.spotimage.catalog.client.ui.SceneQuicklookWindow;
import com.spotimage.catalog.client.ui.component.MyListGrid;
import com.spotimage.catalog.client.ui.listgrid.util.ListActionButton;
import com.spotimage.catalog.client.util.Util;

/**
 * Scene list
 */
public abstract class SceneListGrid extends MyListGrid {
	
	/** used for selection list purpose ? */
	private boolean selectionUse;
	
	/** mouse over window (quicklook, details, ...) */
	private Window mouseOverWindow;

	/** context menu */
	protected Menu contextMenu;
	
	/**
	 * Set list headers and context menu.
	 * @param selectionUse
	 */
	public SceneListGrid(boolean selectionUse) {
		this.selectionUse = selectionUse;
		ListGridField icons = new ListGridField("icons", " ");
		icons.setWidth(60);
		icons.setCanHide(false);
		ListGridField id = new ListGridField("id", "Scene");
		id.setWidth(150);
		id.setHidden(true);
		ListGridField type = new ListGridField("type", "Type");
		type.setAlign(Alignment.RIGHT);
		ListGridField cloudCoverPercentage = new ListGridField("cloudCoverPercentage", "CN");
		cloudCoverPercentage.setAlign(Alignment.RIGHT);
		cloudCoverPercentage.setType(ListGridFieldType.INTEGER);
		cloudCoverPercentage.setCellFormatter(new PercentCellFormatter());
		ListGridField acquisitionDate = new ListGridField("acquisitionDate", "Acquisition");
		acquisitionDate.setType(ListGridFieldType.DATE);
		acquisitionDate.setCellFormatter(new DateCellFormatter());
		setFields(new ListGridField[] { 
					icons, 
					id,
					acquisitionDate,
					type, 
					cloudCoverPercentage
				});
		
		// context menu
		contextMenu = new Menu();
		setContextMenu(contextMenu);
	}

	/**
	 * Populate list with scenes.
	 * @param scenes
	 */
	public void populate(List<SceneListGridRecord> scenes) {
		while (getRecordList().getLength() > 0) {
			removeData(getRecordList().get(0));
		}
		Iterator<SceneListGridRecord> iterator = scenes.iterator();
		while (iterator.hasNext()) {
			addData(iterator.next());
		}
	}

	/**
	 * Override {@link com.smartgwt.client.widgets.grid.ListGrid} to add action buttons for each scene item.
	 */
	@Override
	protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
		final SceneListGridRecord slgr = (SceneListGridRecord) record;
		if (getFieldName(colNum).equals("icons")) {
			HLayout actionIconsLayout = new HLayout(3);
			actionIconsLayout.setHeight(16);
			actionIconsLayout.setWidth(60);
			actionIconsLayout.setAlign(Alignment.LEFT);
			
			// selection
			if (!selectionUse) {
				ListActionButton moveToSelection = new ListActionButton(Util.getImageUrl("cart-add.png"), "Move scene to selection");
				moveToSelection.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						removeData(record);
						EventBus.getInstance().fireEvent(new MoveToSelectionEvent((SceneListGridRecord) record));
					}
				});
				actionIconsLayout.addMember(moveToSelection);
			} else {
				ListActionButton addToSelection = new ListActionButton(Util.getImageUrl("cart-remove.png"), "Remove scene from selection");
				addToSelection.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						removeData(record);
						EventBus.getInstance().fireEvent(new RemoveFromSelectionEvent((SceneListGridRecord) record));
					}
				});
				actionIconsLayout.addMember(addToSelection);
			}
			
			// quicklook
			ListActionButton viewQuicklook = new ListActionButton("[SKIN]/actions/view.png", "Click to pin quicklook of scene");
			viewQuicklook.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					new SceneQuicklookWindow(slgr);
				}
			});
			viewQuicklook.addMouseOverHandler(new MouseOverHandler() {
				//@Override
				public void onMouseOver(MouseOverEvent event) {
					mouseOverWindow = new SceneQuicklookWindow(slgr);
				}
			});
			viewQuicklook.addMouseOutHandler(new MouseOutHandler() {
				//@Override
				public void onMouseOut(MouseOutEvent event) {
					destroyMouseOverWindow();
				}
			});
			actionIconsLayout.addMember(viewQuicklook);

			// details
			ListActionButton viewDetails = new ListActionButton("[SKIN]/actions/groupby.png", "Click to pin details of scene");
			viewDetails.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					new SceneDetailsWindow(slgr);
				}
			});
			viewDetails.addMouseOverHandler(new MouseOverHandler() {
				//@Override
				public void onMouseOver(MouseOverEvent event) {
					mouseOverWindow = new SceneDetailsWindow(slgr);
				}
			});
			viewDetails.addMouseOutHandler(new MouseOutHandler() {
				//@Override
				public void onMouseOut(MouseOutEvent event) {
					destroyMouseOverWindow();
				}
			});
			actionIconsLayout.addMember(viewDetails);
			
			return actionIconsLayout;
		} else {
			return null;
		}
	}
	
	/**
	 * destroy the mouse over window.
	 */
	private void destroyMouseOverWindow() {
		if (mouseOverWindow != null) mouseOverWindow.destroy();
	}
	
	private final class DateCellFormatter implements CellFormatter {
		public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
			if (value != null) {
				try {
					Date dateValue = (Date) value;
					return Util.getDateFormatter().format(dateValue);
				} catch (Exception e) {
					return value.toString();
				}
			} else {
				return "";
			}
		}
	}

	private final class PercentCellFormatter implements CellFormatter {
		public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
			if (value != null) {
				return value.toString() + "%";
			} else {
				return "";
			}
		}
	}
}